package com.concurrent.mydemo;

import java.util.concurrent.locks.ReentrantLock;

public class DemoReentrantLock {
	 static DemoReentrantLock instance = new DemoReentrantLock();
	  private static boolean isRuning = false; // 是否在运行
	  
	  private static ReentrantLock lock = new ReentrantLock();
	  
	  
	  public void run(){
		  System.out.println(Thread.currentThread().getName()+" 进入");
		  
		  //运行完同步块后线程2进入
		 /* synchronized (this) {
			  System.out.println(Thread.currentThread().getName()+" isRuning = "+isRuning+" "+this.toString());
				if (isRuning) {
					System.out.println(Thread.currentThread().getName()+"提示任务正在运行");
					return;
				}
				isRuning=true;
			}*/
		  
		  lock.lock();
		   if(isRuning==true){
			   System.out.println("任务正在运行");
			   lock.unlock();
			   return;
		   }
		   isRuning = true;
		   lock.unlock();
		   
		   System.out.println(Thread.currentThread().getName()+" isRuning = "+isRuning);
		  try{
			  Thread.sleep(10000L);
		  } catch (InterruptedException e) {
			e.printStackTrace();
		}finally{
			  isRuning = false;
		  }
		  System.out.println(Thread.currentThread().getName()+"orver");
	  }
	  
	  public static void main(String[] args) {
		DemoReentrantLock demo = new DemoReentrantLock();
		DemoReentrantLock demo2 = new DemoReentrantLock();
		/*for(int i = 0;i<2;i++){
			new Thread(demo,"线程"+i).start();
		}*/
		Thread t1 = new Thread(new Runnable() {
			@Override
			public void run() {
				demo.run();
			}
		},"线程1");
		Thread t2 = new Thread(new Runnable() {
			
			@Override
			public void run() {
				demo2.run();
			}
		},"线程2");
		t1.start();
		t2.start();
	}
}
